package org.itboys.mobile.controller.web.member;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/12/4
 * 描述：迈迪龙_用户
 */
@Controller
@RequestMapping("/sunday/web/member")
public class SundayMemberController extends BaseController{
    @Autowired
    private SundayMemberService memberService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            @RequestParam(value="returnPage",required=true) Integer returnPage,
            @RequestParam(value="agentId",required=false) Long agentId,
            Model model){
        //代理商
        if(returnPage==1){
            return "/sunday/member/agent/index";
            //销售员
        }else if(returnPage==2){
            model.addAttribute("agentId",agentId);
            return "/sunday/member/sales/index";
        }else if(returnPage==3){
            model.addAttribute("agentId",agentId);
            return "/sunday/member/seller/index";
        }
        return "error";
    }

    /**
     * 详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="userType",required=true) Integer userType,
            Model model){

        SundayMember member = memberService.findOne(id);
        model.addAttribute("member", member);
        if(userType==1){
            return "/sunday/member/agent/input";
        }else if(userType==2){
            return "/sunday/member/sales/input";
        }
        return "error";
    }

    /**
     * 查询
     * @param request
     * @param response
     * @param userTypes
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="userTypes",required=false)List<Integer> userTypes,
                       @RequestParam(value="agentId",required=false)Long agentId) {
        try {

            PageResult<SundayMember> result=memberService.selectMember(request,userTypes,agentId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改
     * @param request
     * @param response
     * @param member
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayMember member){
        try{
            memberService.saveMember(member);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/sunday/web/member/save/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 重置密码
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id){
        try {
            memberService.resetPassword(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/resetPassword------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id){
        try {
            memberService.deleteMember(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 后台审核销售员
     * @param request
     * @param response
     * @param salesManId
     * @param isHandle
     */
    @RequestMapping("/handleSalesMan")
    public void handleSalesMan(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="salesManId",required=true) Long salesManId,
                               @RequestParam(value="isHandle",required=true) int isHandle){
        try {
            memberService.handleSalesMan(salesManId,isHandle);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/handleSalesMan------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 冻结或者启用  销售员和维修工
     * @param request
     * @param response
     * @param memberId
     * @param status
     */
    @RequestMapping("/switchStatus")
    public void switchStatus(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="memberId",required=true) Long memberId,
                               @RequestParam(value="status",required=true) int status){
        try {
            memberService.switchStatus(memberId,status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/switchStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
